package com.wwj.utils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**调用DBUtils执行对应的查询sql语句，并返回查询结果
 *
 */


public class SQLUtils {

    public static void main(String[]args){
        String sql = "SELECT count(*) FROM `member`";
        Object result = query(sql);
        System.out.println(result);


    }
    public static Object query(String sql){
        if(StringUtils.isBlank(sql)){
            return null;

        }
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner();
        //获取数据连接
        Connection conn = JDBCUtils.getConnection();
        try {
            //创建返回结果类型对象
            ScalarHandler rsh = new ScalarHandler();
            //执行sql查询语句
            Object result = qr.query(conn, sql, rsh);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭数据库连接
            JDBCUtils.close(conn);
        }
        return null;


    }





}
